package TwentyOne.Array;


/*
建立一个可以实现整型数组的操作类(Array),而后在里面可以操作的数组的大小由外部来决定,
而后在Array类里面需要提供有数组的如下处理: 
1.进行数据的增加(如果数据满了则无法增加)、
2.可以实现数组的容量扩充、
3.取得数组全部内容。

完成之后在此基础上再派生出两个子类:
数组排序类:返回的数据必须是排序后的结果;
数组反转类: 可以实现内容的首尾交换。
 */
class ArrayOp1 {

    private int[] data;  //整形数组
    private int foot;  //进行数组索引控制

    public ArrayOp1(int len) {
        if (len > 0) {
            this.data = new int[len];   //开辟数组
        } else {
            this.data = new int[1];  //开辟一个空间
        }
    }

    //实现数组的容量扩充，给出的是扩充大小，实际大小：已有大小+扩充大小
    public void increament(int num) {
        int newData[] = new int[this.data.length + num];
        System.arraycopy(this.data, 0, newData, 0, this.data.length);
        this.data = newData;  //改变数组引用
    }

    public boolean add(int num) {  //数据增加
        if (this.foot < this.data.length) { //有位置
            this.data[this.foot++] = num;
            return true;
        }
        return false;
    }

    public int[] getData() {
        return this.data;
    }
}

class SortArray extends ArrayOp1 {  //定义排序子类

    public SortArray(int len) {
        super(len);
    }

    public int[] getData() {   //获取排序结果
        java.util.Arrays.sort(super.getData());   //排序
        return super.getData();
    }
}

class ReverseArray extends ArrayOp1 {  //定义排序子类

    public ReverseArray(int len) {
        super(len);
    }

    public int[] getData() {
        int center = super.getData().length / 2;
        int head = 0;
        int tail = super.getData().length - 1;
        for (int x = 0; x < center; x++) {
            int temp = super.getData()[head];
            super.getData()[head] = super.getData()[tail];
            super.getData()[tail] = temp;
            head++;
            tail--;
        }
        return super.getData();
    }
}

public class a16_ArrayOp1 {

    public static void main(String[] args) {
        System.out.println("-----数组定义-----");
        ArrayOp1 arr = new ArrayOp1(5);
        System.out.println(arr.add(10));
        System.out.println(arr.add(5));
        System.out.println(arr.add(20));
        System.out.println(arr.add(3));
        System.out.println(arr.add(6));
        arr.increament(3);
        System.out.println(arr.add(1));
        System.out.println(arr.add(7));
        System.out.println(arr.add(0));
        for (int ele : arr.getData()) {
            System.out.print(ele+"、");
        }
        System.out.println();

        System.out.println("-----数组排序-----");
        ArrayOp1 arr1 = new SortArray(5);
        System.out.println(arr1.add(10));
        System.out.println(arr1.add(5));
        System.out.println(arr1.add(20));
        System.out.println(arr1.add(3));
        System.out.println(arr1.add(6));
        arr1.increament(3);
        System.out.println(arr1.add(1));
        System.out.println(arr1.add(7));
        System.out.println(arr1.add(0));
        int[] result = arr1.getData();
        for (int ele : result) {
            System.out.print(ele+"、");
        }
        System.out.println();

        System.out.println("-----数组反转-----");
        ArrayOp1 arr2 = new ReverseArray(5);
        System.out.println(arr2.add(10));
        System.out.println(arr2.add(5));
        System.out.println(arr2.add(20));
        System.out.println(arr2.add(3));
        System.out.println(arr2.add(6));
        arr2.increament(3);
        System.out.println(arr2.add(1));
        System.out.println(arr2.add(7));
        System.out.println(arr2.add(0));
        int[] result2 = arr2.getData();
        for (int ele : result2) {
            System.out.print(ele+"、");
        }

        // int [] arr1=new int[]{1,2,3,4,5};
        // for (int ele:arr1) {
        //     System.out.println(ele);
        // }
    }
}
